%% General notes:
%  - Matlab is very well documented: https://www.mathworks.com/help/
%     Get familiar with searching for functions to learn syntax and general
%     usage. They have examples for everything
%  - A semicolon (;) at the end of a statement makes it so that the output
%     doesn't print to the command window when you run the script
%  - Running a script stores variables in your Matlab Workspace. There, you
%     can look at variable values and dimensions.

%% Declaring scalars
v = 5;


%% Declaring arrays
arr1 = 1:10;   % array from 1 to 10 in steps of 1, [1 2 3 ... 10]
arr2 = 1:2:10; % array from 1 to 10 in steps of 2, [1 3 5 7 9]
arr3 = ones(1, 5);  % 1x5 array full of ones, [1 1 1 1 1]
arr4 = zeros(1, 5); % 1x5 array full of zeros, [0 0 0 0 0]


%% Accessing arrays
arr5 = [2 3 5 9 6]; % explicitely define an array
arr5(1) = 7;    % sets entry 1 of arr4 to 7, [7 3 5 9 6]
arr5(end) = 9;  % sets the last entry of arr4 to 10, [7 3 5 9 9]
arr5(3:5) = [1 2 3]; % sets entries 3 through 5 to be [1 2 3], [7 3 1 2 3]


%% Array math
A = [1 2 3];
B = [4 5 6];
C1 = A + B;  % [5 7 9]
C2 = A * 2;  % [2 4 6]
C3 = A / 2;  % [0.5 1 1.5]
C4 = B - 1;  % [3 4 5]
% NOTE: for element-by-element operations put a . before the operator
C5 = A * B'; % 32 (array multiplication of A (1x3) with B transpose (3x1))
C6 = A .* B; % [4 10 18]  
C7 = A .^ 2; % [1 4 9]
C8 = B ./ A; % [4 2.5 2]


%% For-Loops
%  runs the code inside the for-end block for every entry in the 
%  iterator array
for itr = [1, 3, 7] 
    disp(itr) % displays 1; 3; 7
end
for itr = 1:4 
    disp(itr) % displays 1; 2; 3; 4;
end


%% Plots
x = -10:10;
y = 4*x.^2 + 2*x + 1;

figure(1)
plot(x, y)
plot(x, 2*y) % this overwrites the first plot command
plot(x, y, x, 2*y) % this properly overlays both plots
legend('legend 1', 'legend 2')
xlabel('xlabel string')
ylabel('ylabel string')
title('title string')
legend('legend 1')

figure(2)
semilogx(x, y) % x-axis log scale, y-axis linear
figure(3)
semilogy(x, y) % x-axis linear scale, y-axis log
figure(4)
loglog(x, y)   % both axes log scale


%% Transfer Functions
% create Omega, then evaluate the transfer function at every point in Omega
%Omega = logspace(-2, 5, 200); % 200 points between 10^-2 and 10^5
Omega = -1000:1000;
s = 1j * Omega; 
H = 50 * (1 + s/10) ./ ((1 + s/100) .* (1 + s/500)); 

H_magnitude = abs(H); % magnitude of H
H_phase = angle(H);   % phase of H
yyaxis left
plot(Omega, H_magnitude) 
ylabel('Magnitude')
yyaxis right
plot(Omega, H_phase * 180/pi) % convert to degrees
ylabel('Phase (degrees)')

title('Magnitude and Phase response of H(j\Omega)')
legend('Magnitude', 'Phase')
xlabel('\Omega')



%% Functions
% anonymous function:
f = @ex_fun; % handle for the function defined at the end of the script
[a, b] = f(sqrt(2), exp(1), pi); % calls the function using the handle

% m-file function: ex_fun defined in a file called ex_fun.m
%[a, b] = ex_fun(sqrt(2), exp(1), pi); % calls the function using the
                                       %  function name

function [ret1, ret2] = ex_fun(arg1, arg2, arg3)
    ret1 = mean([arg1, arg2, arg3]);
    ret2 = arg1 + arg2 - arg3;
end